import { Controller } from "egg";

export default class BasicFieldController extends Controller {
  public async index() {
    const { ctx } = this;
    const res = await ctx.service.basicField.findList();
    ctx.helper.success({ ctx, res });
  }

  public async create() {
    const { ctx, service } = this;
    const { name } = ctx.request.body;
    const data = await service.basicField.findByName(name);
    if (data) {
      return ctx.helper.error({ ctx, errorType: "commonAlreadyExists" });
    }
    const res = await service.basicField.add(ctx.request.body);
    ctx.helper.success({ ctx, res });
  }

  public async fieldAdd() {
    const { ctx, service } = this;
    const { name, groupId } = ctx.request.body;
    const res = await service.basicField.fieldAdd(groupId, name);
    if (res) {
      ctx.helper.success({ ctx, res });
    } else {
      ctx.helper.error({ ctx, errorType: "commonOperateFail" });
    }
  }
}
